package Presenter;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import Models.CommonBrewSupplies;
import Models.Administration;
import Models.LabType;

public class AdministrationPresenter extends AbstractPresenter<Administration> {
	final String SINGLEITEM = "administration";
	final String MULTIITEM = "administrations";
	
	public ArrayList<Administration> tryJSONParseArray(String input){
		return super.doJSONParseArray(input, SINGLEITEM, MULTIITEM);
	}
	
	public Administration tryJSONParse(String input){
		return super.doJSONParse(input, SINGLEITEM);
	}
	
	public Administration createItemFromJSONObject(JSONObject jsonObject){
		Administration item = new Administration();
		try{
			EmployeePresenter empPresenter = new EmployeePresenter();
			CustomerPresenter custPresenter = new CustomerPresenter();
			
			item.setAdminNumber(jsonObject.getInt("ANum"));
			
			JSONObject jsonApEmp = jsonObject.getJSONObject("apEmp");
			item.setApEmployee(empPresenter.createItemFromJSONObject(jsonApEmp));
			item.setBillingEmailAddress(jsonObject.getString("billingEmail"));
			//item.setBillingEmailPassword(jsonObject.getString("billingEmailPass"));
			item.setBillingEmailPassword(jsonObject.getString("billingEmailPass"));
			
			JSONObject jsonBillingEmp = jsonObject.getJSONObject("billingEmp");
			item.setBillingEmployee(empPresenter.createItemFromJSONObject(jsonBillingEmp));
			item.setbPlato(jsonObject.getBoolean("BPlato"));
			item.setbPOS(jsonObject.getBoolean("bPOS"));
			item.setbQAEnable(jsonObject.getBoolean("BQAEnable"));
			item.setBreweryCity(jsonObject.getString("breweryCity"));
			item.setBreweryCounty(jsonObject.getString("breweryCounty"));
			item.setBreweryEIN(jsonObject.getString("breweryEIN"));
			item.setBreweryEmail(jsonObject.getString("breweryEmail"));
			item.setBreweryName(jsonObject.getString("breweryName"));
			item.setBreweryPhoneNumber(jsonObject.getString("breweryPhoneNumber"));
			item.setBreweryState(jsonObject.getString("breweryState"));
			item.setBreweryStreetAddress(jsonObject.getString("breweryStreetAddress"));
			item.setBreweryWebsite(jsonObject.getString("breweryWebsite"));
			item.setBreweryZipCode(jsonObject.getString("breweryZipCode"));
			item.setCa(jsonObject.getDouble("ca"));
			item.setCl(jsonObject.getDouble("cl"));
			item.setCo3(jsonObject.getDouble("co3"));
			item.setCoTax(jsonObject.getDouble("coTax"));
			item.setDistKegDeposit(jsonObject.getDouble("distKegDeposit"));
			item.setEmailBody(jsonObject.getString("billingEmailBody"));
			item.setEmailSubject(jsonObject.getString("billingEmailSubject"));
			item.setEnableMRP(jsonObject.getBoolean("enableMRP"));
			item.setEnableScan(jsonObject.getBoolean("enableScan"));
			item.setEnableTRScanCheck(jsonObject.getBoolean("enableTRScanCheck"));
			item.setFedTax(jsonObject.getDouble("fedTax"));
			//item.setFooterImt((byte[]) jsonObject.get(""));
			item.setGoogleCalendar(jsonObject.getBoolean("BGoogleCal"));
			//item.setHeaderImg();
			item.setInvEmail(jsonObject.getBoolean("invEmail"));
			item.setInvKegList(jsonObject.getBoolean("invKegList"));
			item.setInvLabel(jsonObject.getBoolean("invLabel"));
			item.setInvPrintOut(jsonObject.getBoolean("invPrintOut"));
			item.setKegPrefix(jsonObject.getString("kegPrefix"));
			item.setMg(jsonObject.getDouble("mg"));
			item.setNa(jsonObject.getDouble("na"));
			item.setNonDistKegDeposit(jsonObject.getDouble("nonDistKegDeposit"));
			item.setNotifyOnLowInv(jsonObject.getBoolean("notifyOnLowInv"));
			item.setPoDays(jsonObject.getInt("daysForPO"));

			JSONObject jsonSalesEmp = jsonObject.getJSONObject("salesEmp");
			item.setSalesEmployee(empPresenter.createItemFromJSONObject(jsonSalesEmp));
			item.setSmtpAddress(jsonObject.getString("SMTPAddress"));
			item.setSmtpAuth(jsonObject.getBoolean("SMTPAuth"));
			item.setSmtpPort(jsonObject.getString("SMTPPort"));
			item.setSmtpStartTLS(jsonObject.getBoolean("SMTPStartTls"));
			item.setSo4(jsonObject.getDouble("so4"));
			item.setStateNumber(jsonObject.getString("breweryStateNumber"));
			item.setSystemSize(jsonObject.getDouble("systemSize"));

			JSONObject jsonTapRoomCustomer = jsonObject.getJSONObject("trCust");
			item.setTrCustomer(custPresenter.createItemFromJSONObject(jsonTapRoomCustomer));
			item.setTtbNumber(jsonObject.getString("breweryTTBNumber"));
			
			//Set Documents Collection
			ArrayList<CommonBrewSupplies> cbss = new ArrayList<CommonBrewSupplies>();
			if(!jsonObject.isNull("cbsCollection")){
				Object itm = jsonObject.get("cbsCollection");
				CommonBrewSuppliesPresenter presenter = new CommonBrewSuppliesPresenter();
				if(itm instanceof JSONArray){
					JSONArray jPSTArray = (JSONArray)itm;
					for(int i=0; i<jPSTArray.length(); i++){
						JSONObject jsonItem = jPSTArray.getJSONObject(i);
						cbss.add(presenter.createItemFromJSONObject(jsonItem));
					}
				} else {
					JSONObject jsonItem = (JSONObject)itm;
					cbss.add(presenter.createItemFromJSONObject(jsonItem));
				}
				item.setCommonBrewSupplies(cbss);
			}
			//Set Inventory Usage Collection
			ArrayList<LabType> labTypes = new ArrayList<LabType>();
			if(!jsonObject.isNull("labTypeCollection")){
				Object itm = jsonObject.get("labTypeCollection");
				LabTypePresenter presenter = new LabTypePresenter();
				if(itm instanceof JSONArray){
					JSONArray jPSTArray = (JSONArray)itm;
					for(int i=0; i<jPSTArray.length(); i++){
						JSONObject jsonItem = jPSTArray.getJSONObject(i);
						labTypes.add(presenter.createItemFromJSONObject(jsonItem));
					}
				} else {
					JSONObject jsonItem = (JSONObject)itm;
					labTypes.add(presenter.createItemFromJSONObject(jsonItem));
				}
				item.setLabTypes(labTypes);
			}
			
			return item;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(Administration item){
		try{
			EmployeePresenter empPresenter = new EmployeePresenter();
			CustomerPresenter custPresenter = new CustomerPresenter();
			
			JSONObject json = new JSONObject();
			json.put("ANum", item.getAdminNumber());
			json.put("apEmp", empPresenter.createJSON(item.getApEmployee()));
			json.put("BGoogleCal", item.isGoogleCalendar());
			json.put("BPlato", item.isbPlato());
			json.put("BQAEnable", item.isbQAEnable());
			json.put("billingEmail", item.getBillingEmailAddress());
			json.put("billingEmailBody", item.getEmailBody());
			json.put("billingEmailPass", item.getBillingEmailPassword());
			json.put("billingEmailSubject", item.getEmailSubject());
			json.put("billingEmp", empPresenter.createJSON(item.getBillingEmployee()));
			json.put("breweryCity", item.getBreweryCity());
			json.put("breweryCounty", item.getBreweryCounty());
			json.put("breweryEIN", item.getBreweryEIN());
			json.put("breweryEmail", item.getBreweryEmail());
			json.put("breweryName", item.getBreweryName());
			json.put("breweryPhoneNumber", item.getBreweryPhoneNumber());
			json.put("breweryState", item.getBreweryState());
			json.put("breweryStateNumber", item.getStateNumber());
			json.put("breweryStreetAddress", item.getBreweryStreetAddress());
			json.put("breweryTTBNumber", item.getTtbNumber());
			json.put("breweryWebsite", item.getBreweryWebsite());
			json.put("breweryZipCode", item.getBreweryZipCode());
			json.put("ca", item.getCa());
			json.put("cl", item.getCl());
			json.put("co3", item.getCo3());
			json.put("coTax", item.getCoTax());
			json.put("daysForPO", item.getPoDays());
			json.put("distKegDeposit", item.getDistKegDeposit());
			json.put("enableMRP", item.isEnableMRP());
			json.put("enableScan", item.isEnableScan());
			json.put("enableTRScanCheck", item.isEnableTRScanCheck());
			json.put("fedTax", item.getFedTax());
			//json.put("footerImg", item.getFooterImage());
			//json.put("headerImg", item.getHeaderImage());
			json.put("invEmail", item.isInvEmail());
			json.put("invKegList", item.isInvKegList());
			json.put("invLabel", item.isInvLabel());
			json.put("invPrintOut", item.isInvPrintOut());
			json.put("kegPrefix", item.getKegPrefix());
			json.put("mg", item.getMg());
			json.put("na", item.getNa());
			json.put("nonDistKegDeposit", item.getNonDistKegDeposit());
			json.put("notifyOnLowInv", item.isNotifyOnLowInv());
			json.put("SMTPAddress", item.getSmtpAddress());
			json.put("SMTPAuth", item.isSmtpAuth());
			json.put("SMTPPort", item.getSmtpPort());
			json.put("SMTPStartTls", item.isSmtpStartTLS());
			json.put("salesEmp", empPresenter.createJSON(item.getSalesEmployee()));
			json.put("so4", item.getSo4());
			json.put("systemSize", item.getSystemSize());
			json.put("trCust", custPresenter.createJSON(item.getTrCustomer()));
			json.put("bPOS", item.isbPOS());

			//Create Doc Collection
			if(item.getCommonBrewSupplies()!=null){
				CommonBrewSuppliesPresenter presenter = new CommonBrewSuppliesPresenter();
				if(item.getCommonBrewSupplies().size()>0){
					JSONArray jsonArray = new JSONArray();
					for(int i=0; i<item.getCommonBrewSupplies().size(); i++){
						jsonArray.put(presenter.createJSON(item.getCommonBrewSupplies().get(i)));
					}
					json.put("cbsCollection", jsonArray);
				}
			}
			//Create Doc Collection
			if(item.getLabTypes()!=null){
				LabTypePresenter presenter = new LabTypePresenter();
				if(item.getLabTypes().size()>0){
					JSONArray jsonArray = new JSONArray();
					for(int i=0; i<item.getLabTypes().size(); i++){
						jsonArray.put(presenter.createJSON(item.getLabTypes().get(i)));
					}
					json.put("labTypeCollection", jsonArray);
				}
			}
			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public Administration getEmptyEntity(){
		Administration entity = new Administration();
		return entity;
	}

}
